package com.his.service.hy;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.his.dao.jpa.BedsDao;
import com.his.dao.jpa.ClinicorderDao;
import com.his.dao.jpa.DepartmentDao;
import com.his.dao.jpa.DrugDao;
import com.his.dao.jpa.EmployeeDao;
import com.his.dao.jpa.HospitalizationDao;
import com.his.dao.jpa.HospitalprescriptionDao;
import com.his.dao.jpa.LeavehospitalDao;
import com.his.dao.jpa.MedicalcardDao;
import com.his.dao.jpa.PatientDao;
import com.his.dao.jpa.PharmacyDetailsDao;
import com.his.dao.jpa.RegistrationDao;
import com.his.dao.jpa.ResidentdoctorDao;
import com.his.pojo.HisBed;
import com.his.pojo.HisClinicorder;
import com.his.pojo.HisDepartment;
import com.his.pojo.HisDrug;
import com.his.pojo.HisEmployee;
import com.his.pojo.HisHospitalization;
import com.his.pojo.HisHospitalprescription;
import com.his.pojo.HisLeavehospital;
import com.his.pojo.HisMedicalCard;
import com.his.pojo.HisPatient;
import com.his.pojo.HisPharmacydetail;
import com.his.pojo.HisPharmacydrugssale;
import com.his.pojo.HisRegistrationForm;
import com.his.pojo.HisResidentdoctor;
import com.his.vo.hy.BedsVo;
import com.his.vo.hy.Brcf;
import com.his.vo.hy.CaoZuo;
import com.his.vo.hy.HisPatientVo;
import com.his.vo.hy.HospitalizationVo;
import com.his.vo.hy.HospitalprescriptionVo;
import com.his.vo.hy.LeavehospitalVo;
import com.his.vo.hy.MedicalcardRegistionVo;

import oracle.net.aso.l;
import oracle.net.aso.p;
import oracle.net.aso.r;
@Service
@Transactional
public class MedicalcardService {
	@Autowired
	private MedicalcardDao medicalcardDao;//医疗卡dao
	@Autowired
	private RegistrationDao registrationDao;//挂号单dao
	@Autowired
	private EmployeeDao employeeDao;//员工表dao
	@Autowired
	private ResidentdoctorDao residentdoctorDao;//住院医生dao
	@Autowired
	private HospitalizationDao hospitalizationDao;//住院通知单dao
	@Autowired
	private DepartmentDao departmentDao;//科室表dao
	@Autowired
	private PatientDao patientDao;//病人基本信息表
	@Autowired
	private BedsDao bedsDao;//病床表dao
	@Autowired
	private HospitalprescriptionDao hospitalprescriptionDao;//住院处方表
	@Autowired 
	private ClinicorderDao  clinicorderDao; //处方明细表dao
	@Autowired
	private LeavehospitalDao leavehospitalDao ;//出院通知单dao
	@Autowired
	private DrugDao drugDao;//药品dao
	@Autowired
	private PharmacyDetailsDao pharmacyDetailsDao;//药品dao

// 查询所有住院通知单
	public Map<String, Object> selectHisHospitalizationAll(int page, int rows) {
		PageRequest pr = new PageRequest(page - 1, rows);

		List<HisHospitalization> list = hospitalizationDao.findHisHospitalizationByPage(pr);

		long total = hospitalizationDao.count();

		Map<String, Object> map = new HashMap<>();

		map.put("total", total);

		map.put("rows", copyHisHospitalization(list));

		return map;

	}

// 复制查询到的住院通知单
	public List<HospitalizationVo> copyHisHospitalization(List<HisHospitalization> list) {
		// 新建一个住院通知单vo集合
		List<HospitalizationVo> hisHospitalizations = new ArrayList<>();
		for (HisHospitalization l : list) {
			
			HospitalizationVo h = new HospitalizationVo();// 新建一个住院通知单vo对象
			long  s = l.getTzState().longValue();
			if(s==1){
			// 查询科室名称
			long ksno = l.getBqNo().longValue();// 将科室号转换成long类型
			HisDepartment ks = departmentDao.findOne(ksno);// 新建一个科室对象保存查询的科室
			String ksmc = ks.getKsName();// 获取科室名
			h.setKsmc(ksmc);// 将科室名写入

			// 查询门诊医生号
			long y = l.getYgNo().longValue();// 将员工号转换成long类型
			HisEmployee empno = employeeDao.findOne(y);// 新建一个员工对象保存查询的员工
			String ysmc = empno.getYgName();// 获取医生姓名
			

			// 查询病人姓名
			long ylkno = l.getYlkNo().longValue();// 将医疗卡号转换成long类型
			HisMedicalCard ylk = medicalcardDao.findOne(ylkno);// 新建一个医疗卡对象保存查询的医疗卡
			String brxm = ylk.getBrName();// 获取病人姓名
				
			h.setBqNo(l.getBqNo());// 将科室号写入
			h.setYgNo(l.getYgNo());// 将门诊医生号写入
			h.setYlkNo(l.getYlkNo());// 将医疗卡号写入
			h.setZyNo(l.getZyNo());// 住院号写入
			h.setYsmc(ysmc);// 将医生姓名写入
			h.setBrxm(brxm);// 将病人姓名写入	
			hisHospitalizations.add(h);// 将住院通知单加入集合
			}
		
		}
		return hisHospitalizations;
	}

// 根据医疗卡号查询
	public Map<String, Object> selceylknot(long ylkno) {
		BigDecimal kh = new BigDecimal(ylkno);
		List<HisHospitalization> list = hospitalizationDao.findByYlkNo(kh);
		Map<String, Object> map = new HashMap<>();
		map.put("total", list.size());
		map.put("rows", copyHisHospitalization(list));
		return map;
	}

// 显示病人的基本信息
	public MedicalcardRegistionVo selectMedicalCard(long ylkno) {
		MedicalcardRegistionVo mrvo = new MedicalcardRegistionVo();// 新建一个病人信息vo对象
		HisMedicalCard medicalCards = medicalcardDao.findOne(ylkno);// 查询医疗卡
		BigDecimal ylk = new BigDecimal(ylkno);// 将医疗卡号转换成BigDecimal类型
		List<HisRegistrationForm> list = registrationDao.findByYlkNo(ylk);// 查询挂号单
		// 判断医疗卡是否存在
		if (medicalCards != null) {
			HisRegistrationForm registrationForm = new HisRegistrationForm();// 新建一个挂号对象
			for (HisRegistrationForm r : list) {
				registrationForm.setBlNo(r.getBlNo());// 将病历号写入
				registrationForm.setGhDate(r.getGhDate());// 将挂号日期写入
				registrationForm.setGhNo(r.getGhNo());// 将挂号号写入
				registrationForm.setGhState(r.getGhState());// 将挂号状态写入
				registrationForm.setKhDepartment(r.getKhDepartment());//
				registrationForm.setGhType(r.getGhType()); //将挂号类型写入
				registrationForm.setKsNo(r.getKsNo());// 将挂号科室号写入
				registrationForm.setYgNo(r.getYgNo());// 将医生号写入
				registrationForm.setYlkNo(r.getYlkNo());// 将医疗卡号写入
			}
		//查询医生姓名	
			long y = registrationForm.getYgNo().longValue();// 将挂号号转换成long类型
			HisEmployee empno = employeeDao.findOne(y);// 新建一个员工对象保存获取到的医师			
			String name = empno.getYgName();//获取医生姓名
		// 查询科室名称
			long ksno = registrationForm.getKsNo().longValue();// 将科室号转换成long类型
			HisDepartment ks = departmentDao.findOne(ksno);// 新建一个科室对象保存查询的科室
			String ksmc = ks.getKsName();// 获取科室名
	
			mrvo.setYgName(name);// 将医生姓名写入mrvo
			mrvo.setBlNo(registrationForm.getBlNo());//将病历号写入mrvo			
			mrvo.setGhDate(registrationForm.getGhDate());//将挂号日期写入mrvo			
			mrvo.setGhNo(registrationForm.getGhNo());//将挂号号写入mrvo			
			mrvo.setGhState(registrationForm.getGhState());//将挂号状态写入mrvo	
			mrvo.setGhType(registrationForm.getGhType());//将挂号类型写入mrvo
			mrvo.setKsNo(registrationForm.getKsNo());// 将科室号写入mrvo
			mrvo.setKhDepartment(ksmc);// 将科室名写入mrvo
			mrvo.setYlkNo(registrationForm.getYlkNo());// 将医疗卡号写入mrvo
			mrvo.setBrAddress(medicalCards.getBrAddress());// 将病人地址写入mrvo
			mrvo.setBrAge(medicalCards.getBrAge());// 将病人年龄写入mrvo
			mrvo.setBrIdcard(medicalCards.getBrIdcard());// 将病人身份证号写入mrvo			
			mrvo.setBrName(medicalCards.getBrName());// 将病人姓名写入mrvo
			mrvo.setBrSex(medicalCards.getBrSex());// 将病人性别写入
		} else {
			mrvo = null;
		}
		return mrvo;
	}
	
//保存住院病人信息
	public void savePatient(MedicalcardRegistionVo br){
		HisPatient hz = new HisPatient();
		
		//hz.setBrNo(2);//保存编号
		hz.setBrDate(br.getRyri()); //保存患者入院日期
		hz.setBrAddress(br.getBrAddress());//保存患者地址
		hz.setBrSex(br.getBrSex());		//保存患者性别
		hz.setBrName(br.getBrName());	//保存患者姓名
		hz.setBrAge(br.getBrAge()); //保存患者年龄
		hz.setBrTel(br.getBrlxfs()); //保存患者联系方式
		hz.setYlkNo(br.getYlkNo());	//保存患者医疗卡号
		hz.setBrIdnumber(br.getBrIdcard());//保存患者身份证号码
		hz.setBqNo(br.getKsNo());//保存患者住院科室
		hz.setBrSituation(br.getBrruqk());//保存患者入院情况
		//查询住院通知单
		hz.setBrMoney(new BigDecimal(0));
		HisHospitalization h =	hospitalizationDao.findByYlkNoAndTzState(br.getYlkNo(), new BigDecimal(1));
		h.setTzState(new BigDecimal(0));//修改住院通知单状态
		hospitalizationDao.save(h);//保存修改的住院通知单
		patientDao.save(hz);//保存病人基本信息表
	}
	
//查询病人入院登记
	public Map<String, Object> selectPatientAll(int page ,int rows){
		PageRequest pr = new PageRequest(page-1, rows);
		List<HisPatient> list  = patientDao.findHisPatientByPage(pr);
		long total = patientDao.count();
		Map<String , Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows",copyPatient(list));
		return map;
	}
	
//复制病人信息
	public  List<HisPatientVo> copyPatient(List<HisPatient> list){
		List<HisPatientVo> l = new ArrayList<>();
		for(HisPatient p:list){
			
		
			if(p.getCwNo() == null ||p.getZyysNo() == null){
			// 查询科室名称
			long ksno = p.getBqNo().longValue();// 将科室号转换成long类型
			HisDepartment ks = departmentDao.findOne(ksno);// 新建一个科室对象保存查询的科室
			String ksmc = ks.getKsName();// 获取科室名	
			HisPatientVo hp = new HisPatientVo();
			hp.setBqNo(p.getBqNo());
			hp.setBrNo(p.getBrNo());
			hp.setBrAge(p.getBrAge());
			hp.setBrDate(p.getBrDate());
			hp.setBrIdnumber(p.getBrIdnumber());
			hp.setBrName(p.getBrName());
			hp.setBrSex(p.getBrSex());
			hp.setBrSituation(p.getBrSituation());
			hp.setBrAddress(p.getBrAddress());
			hp.setKsmc(ksmc);// 将科室名写入
			hp.setBrTel(p.getBrTel());
			hp.setYlkNo(p.getYlkNo());
			hp.setCwNo(p.getCwNo());
			hp.setZycfNo(p.getZycfNo());
			hp.setZyysNo(p.getZyysNo());
			hp.setBrMoney(p.getBrMoney());
			l.add(hp);}
			
		}
		
		return l;
	}
	
//查询病人信息
	public HisPatientVo selectbrxx(long brNo){
		HisPatient p =	patientDao.findOne(brNo);
		HisPatientVo hp = new HisPatientVo();
		String  ksmc = null;
		String zyysmc =null;
		// 查询科室名称
		if(p!=null){
		if(p.getBqNo()!=null){
		HisDepartment ks = departmentDao.findOne(p.getBqNo().longValue());// 新建一个科室对象保存查询的科室
		ksmc = ks.getKsName();// 获取科室名
		if(p.getZyysNo()!=null){
		HisResidentdoctor zyys = residentdoctorDao.findOne(p.getZyysNo().longValue());
		zyysmc = zyys.getZyysName();}
		
		hp.setBqNo(p.getBqNo());
		hp.setBrNo(p.getBrNo());
		hp.setBrAge(p.getBrAge());
		hp.setBrDate(p.getBrDate());
		hp.setBrIdnumber(p.getBrIdnumber());
		hp.setBrName(p.getBrName());
		hp.setBrSex(p.getBrSex());
		hp.setBrSituation(p.getBrSituation());
		hp.setBrAddress(p.getBrAddress());
		hp.setKsmc(ksmc);// 将科室名写入
		hp.setBrTel(p.getBrTel());
		hp.setYlkNo(p.getYlkNo());
		hp.setCwNo(p.getCwNo());
		hp.setZycfNo(p.getZycfNo());
		hp.setZyysNo(p.getZyysNo());
		hp.setZyysmc(zyysmc);
		hp.setBrMoney(p.getBrMoney());
		long zfy = zfy(p);
		hp.setYe(p.getBrMoney().longValue()-zfy);
		}
		else{
			hp=null;}
		}
		return hp;
	}
	
//查询科室床位
	public Map<String , Object> selectkscw(long bqNo){
		
		List<HisBed> beds = bedsDao.findByBqNo(new BigDecimal(bqNo) );
		Map<String, Object> map = new HashMap<>();
		map.put("total", beds.size());
		map.put("rows",copyBeds(beds) );
		return map;
		
	}
	
//复制病床信息
	public List<BedsVo> copyBeds(List<HisBed> beds){
		List<BedsVo> list = new ArrayList<>();
		for(HisBed l:beds){
			long kc = l.getCwEmptybed().longValue();
			if(kc == 0){
			BedsVo bed = new BedsVo();
			bed.setBqNo(l.getBqNo());
			bed.setBqNo(l.getBrNo());
			bed.setCwNo(l.getCwNo());
			bed.setCwRent(l.getCwRent());
			bed.setCwEmptybed(l.getCwEmptybed());
			bed.setZyysNo(bed.getZyysNo());
			bed.setCwid(l.getCwNo());
			list.add(bed);}
		}
		return list;
	}
		
//保存选择的床位
	public CaoZuo  savecw(long brNo,long cwNo){
		CaoZuo c = new CaoZuo();
		HisPatient p = patientDao.findOne(brNo);
		HisBed b = bedsDao.findOne(cwNo);
		if(b!=null||p!=null){
		p.setCwNo(new BigDecimal(b.getCwNo()));
		b.setBrNo(new BigDecimal(p.getBrNo()));
		b.setCwEmptybed(new BigDecimal(1));
		patientDao.save(p);
		bedsDao.save(b);
		c.setReason("选择床位成功");}
		else{
			c.setReason("保存失败");
		}
		return c;
	}
		
//显示科室医生
	public Map<String , Object> selectksys(long bqNo){
		
		List<HisResidentdoctor> list = residentdoctorDao.findByBqNo(new BigDecimal(bqNo));
		Map<String, Object> map = new HashMap<>();
		map.put("total", list.size());
		map.put("rows",copyResidentdoctor(list));
		return map;
		
	}
	
//复制医生
	public List<HisResidentdoctor> copyResidentdoctor(List<HisResidentdoctor> list) {
		List<HisResidentdoctor> l = new ArrayList<>();
		for (HisResidentdoctor r : list) {
			HisResidentdoctor rd = new HisResidentdoctor();
			rd.setBqNo(r.getBqNo());
			rd.setZyysJob(r.getZyysJob());
			rd.setZyysName(r.getZyysName());
			rd.setZyysNo(r.getZyysNo());
			l.add(rd);
		}
		return l;
	}

//保存选择的医生
		public CaoZuo  saveys(long brNo,long ysNo){
			CaoZuo c = new CaoZuo();
			HisPatient p = patientDao.findOne(brNo);
			HisResidentdoctor r = residentdoctorDao.findOne(ysNo);
			if(r!=null||p!=null){
			p.setZyysNo(new BigDecimal(r.getZyysNo()));
			patientDao.save(p);
			c.setReason("选择医生成功");}
			else{
				c.setReason("保存失败");
			}
			return c;
		}
	
//根据姓名查找患者
	public Map<String, Object> selectBrname(String name){
		Map<String , Object> map = new HashMap<>();
		List<HisPatient> p = patientDao.findByBrNameLike("%"+name+"%");
		System.out.println(p);
		map.put("total", p.size());
		map.put("rows", copyPatient(p));
		return map;
		
	}
	
//根据姓名查找患者
	public Map<String, Object> selectYlkno(long ylkNo){
			Map<String , Object> map = new HashMap<>();
			List<HisPatient> p = patientDao.findByYlkNo(new BigDecimal(ylkNo));
			System.out.println(p);
			map.put("total", p.size());
			map.put("rows", copyPatient(p));
			return map;
			
		}
	
//显示床位信息
	public HisPatientVo  selectcw(long cw){
		HisBed bed = bedsDao.findOne(cw);
		return selectbrxx(bed.getBrNo().longValue());
		}
	
	
//根据查询医生所管的病人
	public Map<String, Object> selectysbr(long zyysno){
		
		Map<String , Object > map = new HashMap<>();
		List<HisPatient> patients = patientDao.findByZyysNo(new BigDecimal(zyysno));
 		List<HisPatientVo> brvo = new ArrayList<>();
 		List<HisPatientVo> list = copyWZPatient(patients);
 		for(HisPatientVo   bs:list){
 			if(!(bs.getBrSituation().equals("出院"))){
 				brvo.add(bs);
 			}
 		}
		map.put("total",patients.size());
		map.put("rows",brvo);
		
		return map;
	}
	
//复制登记完整的病人信息
	public  List<HisPatientVo> copyWZPatient(List<HisPatient> list){
		List<HisPatientVo> l = new ArrayList<>();
		for(HisPatient p:list){
			// 查询科室名称
			long ksno = p.getBqNo().longValue();// 将科室号转换成long类型
			HisDepartment ks = departmentDao.findOne(ksno);// 新建一个科室对象保存查询的科室
			String ksmc = ks.getKsName();// 获取科室名
			long zfy = zfy(p);
			
			HisPatientVo hp = new HisPatientVo();
			if(p.getZyysNo()!=null){
			HisResidentdoctor zyys = residentdoctorDao.findOne(p.getZyysNo().longValue());
			String	zyysmc = zyys.getZyysName();
			hp.setZyysmc(zyysmc);}
			hp.setZyf(zfy);//住院总费用
			hp.setYe(p.getBrMoney().longValue()-zfy);
			hp.setBqNo(p.getBqNo());
			hp.setBrNo(p.getBrNo());
			hp.setBrAge(p.getBrAge());
			hp.setBrDate(p.getBrDate());
			hp.setBrIdnumber(p.getBrIdnumber());
			hp.setBrName(p.getBrName());
			hp.setBrSex(p.getBrSex());
			hp.setBrSituation(p.getBrSituation());
			hp.setBrAddress(p.getBrAddress());
			hp.setKsmc(ksmc);// 将科室名写入
			hp.setBrTel(p.getBrTel());
			hp.setYlkNo(p.getYlkNo());
			hp.setCwNo(p.getCwNo());
			hp.setZycfNo(p.getZycfNo());
			hp.setZyysNo(p.getZyysNo());
			hp.setBrMoney(p.getBrMoney());
			l.add(hp);}
			
		
		
		return l;
	}
	
//添加处方
	public void insertzycf(HospitalprescriptionVo hisp){
		HisPatient p = patientDao.findOne(hisp.getHishosp().getBrNo().longValue());
		
		hospitalprescriptionDao.save(hisp.getHishosp());
		List<HisClinicorder> list = hisp.getHisclin();
		if(list!=null){
			for(HisClinicorder l:list){
				l.setZycfNo(new BigDecimal (hisp.getHishosp().getZycfNo()));
				System.out.println(l.getYpcsjgNo());
				//System.out.println(hisp.getHishosp().getZycfNo());
			}
			p.setZycfNo(new BigDecimal(hisp.getHishosp().getZycfNo()));
		patientDao.save(p);
		clinicorderDao.save(list);
		}
	}
	
//查询药品
	public List<HisDrug> selectDrug(){
		List<HisDrug> drugs  = (List<HisDrug>) drugDao.findAll();
		return drugs;
	}
	
//查询出院通知单
	public Map<String, Object> selectLeavehospital(int page, int rows){
		PageRequest pr = new PageRequest(page-1, rows);
		List<HisLeavehospital> list = leavehospitalDao.findHisLeavehospitalByPage(pr);
		Map<String, Object> map = new HashMap<>();
		long total = leavehospitalDao.count();
		map.put("total", total);
		map.put("rows", copyLeavehospital(list));
		return map;
	}
	
//复制出院通知单
	public List<LeavehospitalVo> copyLeavehospital(List<HisLeavehospital> list){
		List<LeavehospitalVo> leavehospitals = new ArrayList<>();
		for(HisLeavehospital cy:list){
			LeavehospitalVo cyvo = new LeavehospitalVo();
			long cyzt = cy.getTzState().longValue();
			if(cyzt == 1){
			cyvo.setBqNo(cy.getBqNo());
			cyvo.setBrNo(cy.getBrNo());
			cyvo.setCyNo(cy.getCyNo());
			cyvo.setTzState(cy.getTzState());
			cyvo.setYgNo(cy.getYgNo());
			cyvo.setYlkNo(cy.getYlkNo());
			HisPatient p = patientDao.findOne(cyvo.getBrNo().longValue());
			cyvo.setBrxm(p.getBrName());
			HisDepartment d = departmentDao.findOne(cyvo.getBqNo().longValue());
			cyvo.setKsmc(d.getKsName());
			cyvo.setCwno(p.getCwNo().longValue());
			HisResidentdoctor r = residentdoctorDao.findOne(cyvo.getYgNo().longValue());
			cyvo.setYsmc(r.getZyysName());
			leavehospitals.add(cyvo);}
		}
		return leavehospitals;
	}
	
//办理入院通知
	public CaoZuo cytz(LeavehospitalVo cytz){
		HisLeavehospital cyd = new HisLeavehospital();
		cyd.setBqNo(cytz.getBqNo());
		cyd.setBrNo(cytz.getBrNo());
		
		cyd.setYgNo(new BigDecimal(cytz.getZyysNo()));
		cyd.setYlkNo(cytz.getYlkNo());
		cyd.setTzState(new BigDecimal(1));
		leavehospitalDao.save(cyd);
		return new CaoZuo(true,"出院通知办理成功");
	}
	
//查询病人
	public HisPatientVo selectbr(long ylkNo){
		List<HisPatient> patients = patientDao.findByYlkNo(new BigDecimal(ylkNo));
		List<HisPatientVo> list = copyWZPatient(patients);
		HisPatientVo p =null;
		if(list.get(0).getZyysNo()!=null){
			p = list.get(0);
			
		}
		return p;
		
	}
	
	/*
	 * public void MCardvoCard(HisMedicalCard medicalCards ,HisMedicalCard
	 * medicalCard){ medicalCard.setBrAddress(medicalCards.getBrAddress());
	 * medicalCard.setBrAge(medicalCards.getBrAge());
	 * medicalCard.setBrIdcard(medicalCards.getBrIdcard());
	 * medicalCard.setBrName(medicalCards.getBrName());
	 * medicalCard.setBrSex(medicalCards.getBrSex());
	 * medicalCard.setYlkNo(medicalCards.getYlkNo()); }
	 */
	/*
	 * public HisRegistrationForm selectRegistration(long ylkno){ BigDecimal ylk
	 * = new BigDecimal(ylkno); List<HisRegistrationForm> list =
	 * registrationDao.findByYlkNo(ylk); List<HisRegistrationForm> l =
	 * RegistrationvoRegistration(list); HisRegistrationForm registrationForm =
	 * new HisRegistrationForm() ; for(HisRegistrationForm r:l){
	 * registrationForm.setBlNo(r.getBlNo());
	 * registrationForm.setGhDate(r.getGhDate());
	 * registrationForm.setGhNo(r.getGhNo());
	 * registrationForm.setGhState(r.getGhState());
	 * registrationForm.setGhType(r.getGhType());
	 * registrationForm.setKhDepartment(r.getKhDepartment());
	 * registrationForm.setKsNo(r.getKsNo());
	 * registrationForm.setYgNo(r.getYgNo());
	 * registrationForm.setYlkNo(r.getYlkNo()); } return registrationForm ;
	 * 
	 * }
	 */
	
 	public List<HisRegistrationForm> RegistrationvoRegistration(List<HisRegistrationForm> list) {
		List<HisRegistrationForm> l = new ArrayList<>();
		for (HisRegistrationForm r : list) {
			HisRegistrationForm registrationForm = new HisRegistrationForm();
			registrationForm.setBlNo(r.getBlNo());
			registrationForm.setGhDate(r.getGhDate());
			registrationForm.setGhNo(r.getGhNo());
			registrationForm.setGhState(r.getGhState());
			registrationForm.setGhType(r.getGhType());
			registrationForm.setKhDepartment(r.getKhDepartment());
			registrationForm.setKsNo(r.getKsNo());
			registrationForm.setYgNo(r.getYgNo());
			registrationForm.setYlkNo(r.getYlkNo());
			l.add(registrationForm);
		}
		return l;

	}
 	
 	
//查询病人住院处方
 	public Brcf selectyp(long brNo){
 		Brcf brcf = new Brcf();
 		HisHospitalprescription cf = hospitalprescriptionDao.findByBrNo(new BigDecimal(brNo));
 		brcf.setYizhu(cf.getZycfDoctor());
 		List<HisClinicorder> cfmx = clinicorderDao.findByzycfNo(new BigDecimal(cf.getZycfNo()));
 		List<String> s = new ArrayList<>();
 		for(HisClinicorder l:cfmx){
 			HisDrug drug = drugDao.findOne(l.getYpcsjgNo().longValue());
 			String yaopin = drug.getCpName();
 			s.add(yaopin);
 		}
 		brcf.setYaopin(s);
 		return brcf; 
 	}
 	
 //办理出院
 	public CaoZuo cysx(long brno,long cyno){
 		HisPatient p = patientDao.findOne(brno);
 		p.setBrSituation("出院");
 		
 	    HisLeavehospital l = leavehospitalDao.findOne(cyno);
 	    l.setTzState(new BigDecimal(0));
 	    HisBed bed = bedsDao.findOne(p.getCwNo().longValue());
 	    bed.setCwEmptybed(new BigDecimal(0));
 	    bed.setBrNo(new BigDecimal(0));
 	    bed.setZyysNo(new BigDecimal(0));
 	    patientDao.save(p);
 	    leavehospitalDao.save(l);
 	    bedsDao.save(bed);
 	    return new CaoZuo("出院手续办理成功");
 	}
 	
 //更改病人情况
 	public CaoZuo updaebrqk(long brNo,String brSituation){
 		HisPatient p = patientDao.findOne(brNo);
 		p.setBrSituation(brSituation);
 		patientDao.save(p);
 		return new CaoZuo(true,"更改成功");
 	}
 	
 //更换床位
 	public CaoZuo ghcw(long brno ,long cwno){
 		HisPatient p = patientDao.findOne(brno);
 		HisBed b = bedsDao.findOne(p.getCwNo().longValue());
 		b.setBrNo(new BigDecimal(0));
 		b.setCwEmptybed(new BigDecimal(0));
 		b.setZyysNo(new BigDecimal(0));
 		p.setCwNo(new BigDecimal(cwno));
 		
 		HisBed nb = bedsDao.findOne(cwno);
 		nb.setBrNo(new BigDecimal(p.getBrNo()));
 		nb.setCwEmptybed(new BigDecimal(1));
 		nb.setZyysNo(p.getZyysNo());
 		bedsDao.save(b);
 		bedsDao.save(nb);
 		patientDao.save(p);
 		return	new CaoZuo(true,"更换床位成功");
 	}
 
 //计算住院费用
 	public long zfy(HisPatient patient){
 
 			//计算病人住院天数
 			Date zyri = patient.getBrDate();
 			Date ri = new Date();
 			long	d = (ri.getTime()-zyri.getTime())/(1000 * 60 * 60 * 24);
 			HisBed bed = bedsDao.findOne(patient.getCwNo().longValue());
 			//住院床位费
 			
 			long cwf = d*bed.getCwRent().longValue();
 		
 			//计算处方费
 			List<HisClinicorder> c = clinicorderDao.findByzycfNo(patient.getZycfNo());
 			long ypf = 0;
 			for(HisClinicorder cl:c){
 				HisPharmacydetail drug = pharmacyDetailsDao.findByKcNo(cl.getYpcsjgNo());
 				ypf = ypf+drug.getYgxqSaleprice().longValue();
 				
 			}
 			 ypf = ypf*d;
 			
 			 
 			long   zfy = ypf + cwf;
 			return zfy;
 	}
 	
//住院缴费
 	public CaoZuo zyjf(long brno,double jfje){
 	 	HisPatient patient = patientDao.findOne(brno);
 	 	double zyzjf = patient.getBrMoney().doubleValue()+jfje;
 	 	patient.setBrMoney(new BigDecimal(zyzjf));
 	 	patientDao.save(patient);
 	 	
 		return  new CaoZuo();
 	}
 
//查询所有住院病人
 	public Map<String, Object> selectzybrall(){		
 		Map<String , Object > map = new HashMap<>();
 		List<HisPatient> patients = (List<HisPatient>) patientDao.findAll();
 		List<HisPatientVo> brvo = new ArrayList<>();
 		List<HisPatientVo> list = copyWZPatient(patients);
 		for(HisPatientVo   bs:list){
 			if(!(bs.getBrSituation().equals("出院"))){
 				brvo.add(bs);
 			}

 		}
 			map.put("total",patients.size());
 			map.put("rows",brvo);
 			
 			return map;
 		}

 //退还出院患者多余费用
 	public CaoZuo  zytf(long brno,double ye){
 		HisPatient patient = patientDao.findOne(brno);
 		double je = patient.getBrMoney().doubleValue()- ye;
 		patient.setBrMoney(new BigDecimal(je));
 		patientDao.save(patient);
 		return	new CaoZuo(true,"退费成功");
 	}
 	

}